'use strict';

angular.module('app')
    .controller('articleWriteController', ['$scope','$rootScope',
        function ($scope,$rootScope) {
            $scope.$on('$viewContentLoaded', function () {
                layui.use(['layedit'], function () {
                    var layedit = layui.layedit;
                    var index = layedit.build('blogBody', {
                        //hideTool: ['image']
                        uploadImage: {
                            url: 'json/upload/demoLayEdit.json'
                            , type: 'get'
                        }
                        //,tool: []
                        , height: 400
                    });

                    getChoose.onclick = function () {
                        alert(layedit.getSelection(index));
                    };

                });

                layui.use(['form'], function () {
                    var form = layui.form;
                    //自定义验证规则
                    form.verify({
                        title: function (value) {
                            if (value.length < 5) {
                                return '标题也太短了吧';
                            }
                        }
                        , pass: [/(.+){6,12}$/, '密码必须6到12位']
                    });
                });
            });
            $scope.article={};
            $scope.submit=function () {
                $scope.article.blogBody=$("#blogBody").val();
                console.log($scope.article.blogHead);
                console.log($scope.article.blogBody);
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/blog',
                    data: angular.toJson($scope.article)})
                    .then(function(result) {
                        console.log('blog post return...');
                        console.log(result);
                        if (result.httpCode == 200) {
                            console.log("blog post success...");
                            alert("成功！");
                        } else {
                            $scope.msg = result.msg;
                            $rootScope.$apply();
                        }
                    });
            }
        }]);